/* @(#)ProjectAsset.java
** 
** Copyright (C) 2012 by Steven D. Stamps
**
**             Trademarks & Copyrights
** Flywheel Management Science(TM), Flywheel Management Model(TM),
** Flywheel Story Editor(TM) and FlywheelMS(TM) are exclusive trademarks
** of Steven D. Stamps and may only be used freely for the purpose of
** identifying the unforked version of this software.  Subsequent forks
** may not use these trademarks.  All other rights are reserved.
**
** DecKanGL (Decorated Kanban Glyph Language) and TribKn (Tribal Knowledge)
** are also exclusive trademarks of Steven D. Stamps.  These may be used
** freely within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** gConGUI (game Controller Graphical User Interface) is an exclusive
** trademark of Steven D. Stamps.  This trademark may be used freely
** within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** Trademark information is available at
** <http://www.flywheelms.com/trademarks>
**
** Flywheel Management Science(TM) is a copyrighted body of management
** metaphors, governance processes, and leadership techniques that is
** owned by Steven D. Stamps.  These copyrighted materials may be freely
** used, without alteration, by the community (users and developers)
** surrounding this GPL3-licensed software.  Additional copyright
** information is available at <http://www.flywheelms.org/copyrights>
**
**              GPL3 Software License
** This program is free software: you can use it, redistribute it and/or
** modify it under the terms of the GNU General Public License, version 3,
** as published by the Free Software Foundation. This program is distributed
** in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
** even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
** PURPOSE.  See the GNU General Public License for more details. You should
** have received a copy of the GNU General Public License, in a file named
** COPYING, along with this program.  If you cannot find your copy, see
** <http://www.gnu.org/licenses/gpl-3.0.html>.
*/

package com.flywheelms.library.fmm.node.impl.governable;

import java.util.Collection;

import com.flywheelms.library.fmm.FmmDatabaseMediator;
import com.flywheelms.library.fmm.node.NodeId;
import com.flywheelms.library.fmm.node.impl.commitment.StrategicCommitment;
import com.flywheelms.library.fmm.node.impl.completable.FmmCompletableNodeImpl;
import com.flywheelms.library.fmm.node.impl.enumerator.FmmNodeDictionary;
import com.flywheelms.library.fmm.perspective.FmmPerspective;
import com.flywheelms.library.fmm.transaction.FmmNodeGlyphType;

public class ProjectAsset extends FmmCompletableNodeImpl
		implements Comparable<ProjectAsset> {
	
	private static final long serialVersionUID = -3645381005646011918L;
	private StrategicCommitment strategicCommitment;
	private String flywheelTeamNodeIdString;
	private FlywheelTeam flywheelTeam;
	
	//TODO - review this String class member
	private String flywheelTeamId;
	

	public ProjectAsset(NodeId aNodeId) {
		super(aNodeId);
	}
	
	public ProjectAsset(String aExistingNodeIdString) {
		super(NodeId.rehydrate(
				ProjectAsset.class,
				aExistingNodeIdString ));
		// TODO Auto-generated constructor stub
	}

	public String getFlywheelTeamNodeId() {
		return this.flywheelTeamNodeIdString;
	}

	public void setFlywheelTeamNodeId(String flywheelTeamNodeId) {
		this.flywheelTeamNodeIdString = flywheelTeamNodeId;
		this.flywheelTeam = null;
	}

	public FlywheelTeam getFlywheelTeam() {
		if(this.flywheelTeam == null) {
			this.flywheelTeam = FmmDatabaseMediator.getActiveMediator().getFlywheelTeam(this.flywheelTeamNodeIdString);
		}
		return this.flywheelTeam;
	}

	public void setFlywheelTeam(FlywheelTeam flywheelTeam) {
		this.flywheelTeam = flywheelTeam;
		this.flywheelTeamNodeIdString = this.flywheelTeam.getNodeIdString();
	}

	public String getStrategicMilestoneNodeId() {
		return getStrategicCommitment() == null ? null :
			getStrategicCommitment().getStrategicMilestoneNodeId();
	}

	public void setStrategicMilestoneNodeId(String aStrategicMilestoneNodeId) {
		if(getStrategicCommitment() != null) {
			getStrategicCommitment().setStrategicMilestoneNodeId(aStrategicMilestoneNodeId);
		} else {
			setStrategicCommitment(new StrategicCommitment(getNodeIdString(), aStrategicMilestoneNodeId));
		}
	}

	public StrategicCommitment getStrategicCommitment() {
		if(this.strategicCommitment == null) {
			this.strategicCommitment = FmmDatabaseMediator.getActiveMediator().getStrategicCommitment(getNodeIdString());
		}
		return this.strategicCommitment;
	}

	public void setStrategicCommitment(StrategicCommitment aStrategicCommitment) {
		this.strategicCommitment = aStrategicCommitment;
	}

//	@Override
//	protected void initializeNodeSummaryMap() {
//		super.initializeNodeSummaryMap();
//		NodeSummary theStrategicPlanningSummary = new NodeSummary();
//		theStrategicPlanningSummary.setSummaryDrawableResourceId(
//				FmmNodeDictionary.PROJECT_ASSET.getUndecoratedGlyphResourceId(FmmNodeGlyphType.GREEN) );
//		updateNodeSummary(FmmPerspective.STRATEGIC_PLANNING, theStrategicPlanningSummary);
//		this.nodeSummaryMap.put(FmmPerspective.STRATEGIC_PLANNING, theStrategicPlanningSummary);
//	}

	@Override
	protected void initializeNodeCompletionSummaryMap() {
		super.initializeNodeCompletionSummaryMap();
		NodeCompletionSummary theStrategicPlanningSummary = new NodeCompletionSummary();
		theStrategicPlanningSummary.setSummaryDrawableResourceId(
				FmmNodeDictionary.WORK_PACKAGE.getUndecoratedGlyphResourceId(FmmNodeGlyphType.GREEN) );
		updateNodeCompletionSummary(FmmPerspective.STRATEGIC_PLANNING, theStrategicPlanningSummary);
		this.nodeCompletionSummaryMap.put(FmmPerspective.STRATEGIC_PLANNING, theStrategicPlanningSummary);
	}

	@Override
	public void updateNodeCompletionSummary(FmmPerspective anFmmPerspective, NodeCompletionSummary aNodeSummary) {
		switch(anFmmPerspective) {
		case STRATEGIC_PLANNING:
			Collection<WorkPackage> theWorkPackageCollection = getWorkPackageCollection();
			if(theWorkPackageCollection.size() > 0) {
				aNodeSummary.setShowNodeSummary(true);
				aNodeSummary.setSummaryPrefix("( " + countGreenWorkPackages() + " ");
				aNodeSummary.setSummarySuffix(" of " + theWorkPackageCollection.size() + " )");
			} else {
				aNodeSummary.setShowNodeSummary(false);
			}
			break;
		default:
			break;
		}
	}

	private Collection<WorkPackage> getWorkPackageCollection() {
		return FmmDatabaseMediator.getActiveMediator().getWorkPackageCollectionForProjectAsset(this);
	}

	private int countGreenWorkPackages() {
		int theGreenCount = 0;
		for(WorkPackage theWorkPackage : getWorkPackageCollection()) {
			if(theWorkPackage.isGreen()) {
				++theGreenCount;
			}
		}
		return theGreenCount;
	}

	public void setFlywheelTeamId(String string) {
		this.flywheelTeamId = string;
	}
	
	public String getFlywheelTeamId()
	{
		return this.flywheelTeamId;
	}

	@Override
	public int compareTo(ProjectAsset anOtherProjectAsset) {
		return (getSequence() < anOtherProjectAsset.getSequence() ? -1 :
			(getSequence() == anOtherProjectAsset.getSequence() ? 0 : 1));
	}

}
